//
//  XQPositionCompanyDetailViewController.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/12/2.
//  Copyright © 2016年 校企桥. All rights reserved.
//
import Alamofire
import ObjectMapper
import AlamofireObjectMapper
import SwiftyJSON
import UIKit

class XQPositionCompanyDetailViewController: XQBaseViewController,UITableViewDataSource,UITableViewDelegate {

    var cid = String()
    var type = String()
    
    
    private  var tableView = UITableView()
    private var companyInfoMode:XQPositionCompanyInfoMode?
    private var companyJobMode:XQPositionCompanyjobMode?
    private var tableHeaderView = XQPositionCompanyDetailTableHeaderView()
    private var isJob = Bool()
    
    private var  XQPositionCompanyTableViewCellID = "XQPositionCompanyTableViewCellID"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "企业空间"
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    override func setRootView() {
        self.view.backgroundColor = UIColor.groupTableViewBackground
        
        tableHeaderView = XQPositionCompanyDetailTableHeaderView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 130))
        self.view.addSubview(tableHeaderView)
        
        self.tableView = UITableView.init(frame:  CGRect.zero, style: .plain)
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(self.tableView)
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.register(XQPositionCompanyTableViewCell.self, forCellReuseIdentifier: XQPositionCompanyTableViewCellID)
        tableView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
            make.top.equalTo(tableHeaderView.snp.bottom).offset(5)
            make.bottom.equalTo(self.view.snp.bottom)
        }
        NotificationCenter.default.addObserver(self, selector: #selector(self.requestcompanyIntro), name: NSNotification.Name(rawValue: "companyIntro"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.requestcompanyRecruit), name: NSNotification.Name(rawValue: "companyRecruit"), object: nil)
        
        requestcompanyIntro()
    }
    
    @objc private func requestcompanyIntro() {
        isJob = false
        let urlstr = MTBaseURL.appending("NewVerRecruit/companyIntro.json")
        
        let para: Parameters = ["cid":cid]
        
        Alamofire.request(urlstr,parameters:para).responseObject { (response:DataResponse<XQPositionCompanyInfoMode>) in
            self.companyInfoMode = response.result.value
            self.tableHeaderView.companyInfoMode = self.companyInfoMode?.ci
            self.tableView.reloadData()
        }
        
    }

    @objc private func requestcompanyRecruit() {
        isJob = true
        
        let urlstr = MTBaseURL.appending("NewVerRecruit/companyRecruit.json")
        
        let para: Parameters = ["cid":cid,"type":""]
        
        Alamofire.request(urlstr,parameters:para).responseObject { (response:DataResponse<XQPositionCompanyjobMode>) in
            self.companyJobMode = response.result.value
            self.tableView.reloadData()
        }
        
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if isJob == true {
            return 1
        }else{
            return 2
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isJob == true {
            return (self.companyJobMode?.rs?.count)!
        }else{
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if isJob == true {
            return 10
        }else{
            return 30
        }
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if isJob == true {
            return ""
        }else{
            if section == 0 {
                return "企业形像"
            }else{
                return "企业简介"
            }
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if isJob == true{
            return 50
        }else{
            if indexPath.section == 0 {
                return 40
            }else{
                if ((companyInfoMode?.ci?.introduction) != nil) {
                let size = companyInfoMode?.ci?.introduction?.boundingRect(with:CGSize.init(width: SCREEN_WIDTH-30, height: CGFloat.greatestFiniteMagnitude), options: [NSStringDrawingOptions.usesFontLeading,NSStringDrawingOptions.usesLineFragmentOrigin], attributes: nil, context: nil);
                
                    if (size?.height)! > 40 {
                        return CGFloat((size?.height)!)+20
                    }else{
                        return 40
                    }
                }else{
                    return 40
                }
            
            }
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if isJob == true {
            let jobCell = tableView.dequeueReusableCell(withIdentifier: XQPositionCompanyTableViewCellID) as!XQPositionCompanyTableViewCell
            jobCell.companyJobMode = self.companyJobMode?.rs?[indexPath.row]
            return jobCell
        }else{
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell != nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
            }
            cell?.selectionStyle = .none
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 12)
            cell?.textLabel?.numberOfLines = 0
            if indexPath.section == 0 {
            cell?.textLabel?.text = "有待确认"
            }else{
               
                cell?.textLabel?.text = self.companyInfoMode?.ci?.introduction
            
            }
            return cell!
        }
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }

}
